package com.mz.ieann.aide.main

import android.content.Intent
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.widget.Toolbar
import androidx.core.content.ContextCompat
import androidx.drawerlayout.widget.DrawerLayout
import androidx.navigation.Navigation
import androidx.navigation.ui.AppBarConfiguration
import androidx.navigation.ui.NavigationUI
import com.google.android.material.navigation.NavigationView
import com.mz.ieann.aide.R
import com.mz.ieann.aide.base.BaseActivity
import com.mz.ieann.aide.dialog.PermissionsHelper
import com.mz.ieann.aide.main.flutter.FlutterTestActivity
import com.mz.ieann.aide.media.audio.view.AudioPlayerActivity
import com.mz.ieann.aide.media.svga.RewardActivity
import com.mz.ieann.aide.media.svga.SvgaLoadTestActivity

/**
 * <p>------------------------------------------------------
 * <p> Copyright (C) 2021 zhangmz, All rights reserved.
 * <p> If there is technical discussion, you can contact zhangmz90@foxmail.com
 * <p>------------------------------------------------------
 * <p> des
 * <p>
 *
 * @author Created by Administrator
 * @date on 2021 2021/11/14 11:55
 */
class MainActivity : BaseActivity() {

    private var mAppBarConfiguration: AppBarConfiguration? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        initView()
        initMember()
    }

    /**
     * 初始化底部导航
     */
    private fun initView(){
        val toolbar = findViewById<Toolbar>(R.id.toolbar)
        setSupportActionBar(toolbar)
        val drawer = findViewById<DrawerLayout>(R.id.drawerLayout)
        val navigationView = findViewById<NavigationView>(R.id.navView)
        mAppBarConfiguration = AppBarConfiguration.Builder(
            R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow
        )
            .setDrawerLayout(drawer)
            .build()
        val navController = Navigation.findNavController(this, R.id.nav_host_fragment)
        NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration!!)
        NavigationUI.setupWithNavController(navigationView, navController)
    }

    private fun initMember(){
        val navHeader : View = findViewById<NavigationView>(R.id.navView).getHeaderView(0)
        val photo : ImageView = navHeader.findViewById(R.id.member_photo)
        photo.setImageDrawable(ContextCompat.getDrawable(this, R.mipmap.photo))
        val name : TextView = navHeader.findViewById(R.id.member_name)
        name.text = "明"
        val emailView : TextView = navHeader.findViewById(R.id.member_email)
        val email = "zhangmz90@foxmail.com"
        emailView.text = email
    }

    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.activity_main_options, menu)
        return true
    }

    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<out String>,
        grantResults: IntArray
    ) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        PermissionsHelper.requestPermissionResult(this, requestCode, permissions, grantResults)
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when (item.itemId) {
            R.id.action_video -> {
                startActivity(Intent(this, SvgaLoadTestActivity::class.java))
            }
            R.id.action_flutter -> {
                startActivity(Intent(this, FlutterTestActivity::class.java))
            }
            R.id.action_settings -> {
                startActivity(Intent(this, RewardActivity::class.java))
            }
            R.id.action_about -> {
                startActivity(Intent(this, AudioPlayerActivity::class.java))
            }
            else -> this.toast("other")
        }
        return super.onOptionsItemSelected(item)
    }

    override fun onSupportNavigateUp(): Boolean {
        val navController = Navigation.findNavController(this, R.id.nav_host_fragment)
        return (NavigationUI.navigateUp(navController, mAppBarConfiguration!!)
                || super.onSupportNavigateUp())
    }

}